package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.JavaBean.User;
import com.example.neusoft.yuejian_12.Listener.UserListener;
import com.example.neusoft.yuejian_12.Model.UserModel;
import com.example.neusoft.yuejian_12.R;

//import com.example.neusoft.yuejian_12.Common.utilMeth;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,UserListener {
    private Button btn_reg, btn_login;
    private EditText username, pass;
    private Switch sw_remember;
    private User loginResult;
    SharedPreferences sp;
    String uname,tel;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        btn_login.setOnClickListener(this);
    }

    public void initViews(){
        btn_login=(Button)findViewById(R.id.logbtn_login);
        btn_reg=(Button)findViewById(R.id.logbtn_register);
        username=(EditText)findViewById(R.id.logtelephone);
        pass=(EditText)findViewById(R.id.logpassword);
        sw_remember = (Switch)findViewById(R.id.sw);
        sp=getSharedPreferences("loginResult",MODE_PRIVATE);
        readSP();
    }

    private void readSP() {
        String uname = sp.getString("username",null);
        String pswd = sp.getString("password",null);
        Boolean remember = sp.getBoolean("remember",false);
        if (remember){
            username.setText(uname);
            pass.setText(pswd);
            sw_remember.setChecked(remember);
        }
    }

    public void initEvents(){
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (username.getText().toString().trim().equals("")||pass.getText().toString().trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空",
                            Toast.LENGTH_SHORT).show();
                } else{
                    login();
                }
            }
        });
    }

    private void login() {
        UserModel userModel = new UserModel();
        userModel.Login(username.getText().toString().trim(), pass.getText().toString().trim(),this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logbtn_login:
                uname = username.getText().toString();
                pwd = pass.getText().toString();
                saveSP();
                loginResult = new User();
                UserModel userModel = new UserModel();
                userModel.Login(uname, pwd, this);
                break;
        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        String uname = username.getText().toString();
        editor.putString("username",uname);
        String pwd = pass.getText().toString();
        editor.putString("password",pwd);
        Boolean remember = sw_remember.isChecked();
        editor.putBoolean("remember",remember);
        editor.commit();
    }

    @Override
    public void onResponse(User userbean) {
        saveUser(userbean.getId(),userbean.getTel(),userbean.getUser_name());
        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "fail"+msg, Toast.LENGTH_SHORT).show();
    }

    public void saveUser(String id,String tel,String uname) {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("username", username.getText().toString());
        editor.putString("user_id", id);
        editor.putString("user_tel",tel);
        editor.putString("user_name",uname);
        editor.putString("userpass", pass.getText().toString());
        editor.commit();//提交修改
    }

}






